package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname CategoryController
 * @Description TODO
 * @Date 2023/3/2 14:33
 * @Created by 11404
 */

@Slf4j
@Api(tags = "菜品分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    //查询菜品分类表
    @ApiOperation(value = "分页查询所有菜品分类")
    @Cacheable(value = "category")
    @GetMapping("/findAll")
    public ResultInfo findAll() {

//        List<Category> list = null;
//
//        // 先查询缓存
//        String categoryKey = "category_list";
//
//        if (redisTemplate.hasKey(categoryKey)) {
//            // yes redis有数据
//            list = (List<Category>) redisTemplate.opsForValue().get(categoryKey);
//            log.info("查询redis缓存");
//        } else {
//            // no redis没有数据
//            // 调用service查询MySQL
//            list = categoryService.findAllCategory();
//            log.info("查询MySQL数据库");
//            // 同步到redis中
//            redisTemplate.opsForValue().set(categoryKey, list);
//        }


        List<Category> list = categoryService.findAllCategory();

        // 返回结果
        return ResultInfo.success(list);
    }


    //新增菜品分类
    @ApiOperation(value = "新增菜品分类")
    @CacheEvict(value = "category",key ="#category.id")
    @PostMapping
    public ResultInfo save(@RequestBody Category category) {

        // 2.调用service新增
        categoryService.saveCategory(category);

        // 清理缓存
        redisTemplate.delete("category_list");

        // 3.返回结果
        return ResultInfo.success();

    }

    //修改菜品分类
    @ApiOperation(value = "修改菜品分类")
    @CacheEvict(value = "category",allEntries = true)
    @PutMapping
    public ResultInfo update(@RequestBody Category category){  // 1.接收参数

        // 2.调用service修改
        categoryService.updateCategory(category);

        // 清理缓存
        redisTemplate.delete("category_list");

        // 3.返回结果
        return ResultInfo.success();
    }

    //删除菜品分类
    @ApiOperation(value = "删除菜品分类")
    @CacheEvict(value = "category",allEntries = true)
    @DeleteMapping
    public ResultInfo deleteById(Long id) {// 1.接收参数

        // 2.调用service删除
         categoryService.deleteCategory(id);

        // 清理缓存
        redisTemplate.delete("category_list");

        // 3.直接返回
        return ResultInfo.success();
    }

    //菜品分类回显
    @ApiOperation(value = "根据type查询菜品分类")
    @Cacheable(value = "dish",key = "#type")
    @GetMapping("/list")
    public ResultInfo CategoryById(Integer type){

        return ResultInfo.success(categoryService.CategoryId(type));

    }


}
